let app = new Vue({
    el: '#app',
    data: {
        admin: JSON.parse(sessionStorage.getItem("admin")),
        paperQuestionId: sessionStorage.getItem('singleUpdateId'),
        paperQuestion: [],
        difficult: 0
    },
    created() {

        layui.use('form', function () {

            // 查询年级
            $.ajax({
                url: 'http://localhost:8080/subject/selectLevelNameByDistinct',
                type: 'get',
                success: function (vo2) {
                    if (vo2.code == 200) {

                        $.each(vo2.data, function (i, s) {
                            $("#levelName").append("<option value='" + s.levelName + "'>" + s.levelName + "</option>");
                        });
                        layui.form.render();

                        // 根据会话的试题ID查询试题信息
                        $.ajax({
                            url: 'http://localhost:8080/paperQuestion/select',
                            type: 'get',
                            data: { paperQuestionId: app.paperQuestionId },
                            success: function (vo) {
                                if (vo.code == 200) {
                                    app.paperQuestion = vo.data[0];
                                    app.difficult = vo.data[0].difficult;

                                    // 渲染评分
                                    layui.rate.render({
                                        elem: '#test1',
                                        value: vo.data[0].difficult,  // 初始值
                                        choose: function (value) {
                                            app.difficult = value;
                                        }
                                    })

                                    $("#title").val(vo.data[0].questionContentInfo.title);
                                    $("#questionA").val(vo.data[0].questionContentInfo.questionA)
                                    $("#questionB").val(vo.data[0].questionContentInfo.questionB)
                                    $("#questionC").val(vo.data[0].questionContentInfo.questionC)
                                    $("#questionD").val(vo.data[0].questionContentInfo.questionD)
                                    $("#score").val(vo.data[0].score)
                                    $("#resolution").val(vo.data[0].questionContentInfo.resolution)

                                    $("#levelName option").each(function () {
                                        if ($(this).val() == vo.data[0].levelName) {
                                            $(this).prop('selected', 'selected');
                                        }
                                    });
                                    $("#subjectName option").each(function () {
                                        if ($(this).val() == vo.data[0].subjectName) {
                                            $(this).prop('selected', 'selected');
                                        }
                                    });

                                    if (vo.data[0].correctAnswer == vo.data[0].questionContentInfo.questionA) {
                                        $("input[name='correctAnswer']").each(function () {
                                            if ($(this).val() == 'a') {
                                                $(this).prop('checked', 'checked');
                                            }
                                        });
                                    } else if (vo.data[0].correctAnswer == vo.data[0].questionContentInfo.questionB) {
                                        $("input[name='correctAnswer']").each(function () {
                                            if ($(this).val() == 'b') {
                                                $(this).prop('checked', 'checked');
                                            }
                                        });
                                    } else if (vo.data[0].correctAnswer == vo.data[0].questionContentInfo.questionC) {
                                        $("input[name='correctAnswer']").each(function () {
                                            if ($(this).val() == 'c') {
                                                $(this).prop('checked', 'checked');
                                            }
                                        });
                                    } else if (vo.data[0].correctAnswer == vo.data[0].questionContentInfo.questionD) {
                                        $("input[name='correctAnswer']").each(function () {
                                            if ($(this).val() == 'd') {
                                                $(this).prop('checked', 'checked');
                                            }
                                        });
                                    }
                                    // 渲染表单
                                    layui.form.render();

                                }

                            }
                        });
                    }
                }
            });

        });

    },
    methods: {
        save: function () {
            // 非空判断
            if ($("#title").val() == '' || $("#resolution").val() == '' || $("#levelName").val() == '' || $("#subjectName").val() == '' || $("#questionA").val() == '' || $("#questionB").val() == '' || $("#questionC").val() == '' || $("#questionD").val() == '' || $("input[name='correctAnswer']:checked").val() == null || $("#score").val() == '') {
                layer.msg("必填字段不能为空", { offset: 't', icon: 5 });
                return;
            }

            let correctAnswer;
            if ($("input[name='correctAnswer']:checked").val() == 'a') {
                correctAnswer = $("#questionA").val();
            }
            if ($("input[name='correctAnswer']:checked").val() == 'b') {
                correctAnswer = $("#questionB").val();
            }
            if ($("input[name='correctAnswer']:checked").val() == 'c') {
                correctAnswer = $("#questionC").val();
            }
            if ($("input[name='correctAnswer']:checked").val() == 'd') {
                correctAnswer = $("#questionD").val();
            }


            $.ajax({
                url: 'http://localhost:8080/questionContent/update',
                type: 'post',
                data: { questionContentId: this.paperQuestion.questionContentId, title: $("#title").val(), questionA: $("#questionA").val(), questionB: $("#questionB").val(), questionC: $("#questionC").val(), questionD: $("#questionD").val(), resolution: $("#resolution").val() },
                success: function (vo) {
                    if (vo.code == 200) {

                        let formData = {
                            'subjectName': $("#subjectName").val(),
                            'levelName': $("#levelName").val(),
                            'score': $("#score").val(),
                            'difficult': app.difficult,
                            'correctAnswer': correctAnswer,
                            'paperQuestionId': app.paperQuestion.paperQuestionId
                        }

                        $.ajax({
                            url: 'http://localhost:8080/paperQuestion/update',
                            type: 'post',
                            data: formData,
                            success: function (vo2) {
                                if (vo2.code == 200) {
                                    layer.msg(vo2.data, { offset: 't', icon: 1 });
                                    // 延迟1秒执行代码
                                    setTimeout(function () {
                                        xadmin.close();
                                        xadmin.father_reload()
                                    }, 1000);
                                    
                                } else {
                                    layer.msg(vo2.message, { offset: 't', icon: 2 });
                                }
                            }
                        });

                    }
                }
            });
        },
        test: function () {
            $("#title2").text($("#title").val());
            $("#questionA2").text($("#questionA").val());
            $("#questionB2").text($("#questionB").val());
            $("#questionC2").text($("#questionC").val());
            $("#questionD2").text($("#questionD").val());
            layer.open({
                type: 1,
                title: ['预览', 'font-size:16px'],
                anim: 5,   // 动画
                offset: '50px',  // 坐标
                resize: false,  // 是否允许拉伸改变宽高
                area: ['auto'], // 设置宽，高
                shadeClose: true, //点击遮罩关闭
                scrollbar: false,   // 屏蔽浏览器滚动（false: 则屏蔽）
                content: $("#show-test")
            });
        }
    }
});